package com.wengu.rabbitmq.direct;

import com.rabbitmq.client.*;
import com.wengu.rabbitmq.util.ConnectionUtil;

import java.io.IOException;

/**
 * @author LoserJohn
 * @create 2020-09-07 15:04
 */
//消费者
public class Recv1 {
    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("direct.queue.recv1", false, false, false, null);
        //给队列绑定交换机
        //参数1：队列名称，参数2：交换机名称，参数3：路由key(交换机会根据此key决定数据是否呀要路由给当前队列)
        //关心商品的增删改
        channel.queueBind("direct.queue.recv1", Send.EXCHANGE_NAME, "goods.insert");
        channel.queueBind("direct.queue.recv1", Send.EXCHANGE_NAME, "goods.update");
        channel.queueBind("direct.queue.recv1", Send.EXCHANGE_NAME, "goods.delete");
        //监听队列
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("[ RECV1 ] 接受到消息： " + new String(body));
            }
        };
        channel.basicConsume("direct.queue.recv1", true, consumer);
    }
}
